import threading
import logging

logging.basicConfig(level=logging.INFO)

cups = []

# 实例化锁，10个工人一把锁
lock = threading.Lock()


# 每个工人制作杯子
def worker(lock: threading.Lock, num):
    while True:
        # 拿锁
        lock.acquire()

        # 统计多少个cups，打印出来。
        count = len(cups)
        logging.info('worker is {} ,{} cups has been made'.format(threading.current_thread().name, count))

        # 如果没做够，继续制作杯子
        if count == num:
            # 杯子制作够了，解锁然后退出
            lock.release()
            break
        else:
            # 一个杯子制作完毕，解锁
            cups.append(1)
            lock.release()
            logging.info("{} make 1".format(threading.current_thread().name))
    logging.info("worker is {} , {} cups has finished...".format(threading.current_thread().name, len(cups)))


for x in range(10):
    threading.Thread(target=worker, args=(lock, 20)).start()
